package com.aye.notepad.Activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.aye.notepad.DatabaseModel.User;
import com.aye.notepad.R;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private Button bt_register,bt_login;
    private EditText et_name,et_pwd;
    private Switch cb_pwd;
    AlertDialog alertDialog;
    private String inName,inPwd,findName,findPwd,findName1,findPwd1;
    private static final String TAG = "LoginActivity";
    private int userId;
    private SharedPreferences sp1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        //创建数据库
        Connector.getDatabase();
    }
    /*点击显示按钮*/
    public void show(View v){
        //如果选中，则显示密码
        if (cb_pwd.isChecked()==true){
            et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }//否则隐藏密码
        else {
            et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }
    /*点击注册按钮*/
    public void register(View v) {
        startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
    }
/*点击登录按钮*/
    public void login(View v){
        inName=et_name.getText().toString();//输入框中的用户名
        inPwd=et_pwd.getText().toString();//输入框中的密码
        List<User> userList=DataSupport.where("name=?",inName).find(User.class);
        for (User user:userList) {
            findName = user.getName();
            findPwd = user.getPassword();
            userId=user.getId();
            Log.i(TAG, "login: " + user.toString());
        }
        sp1=getSharedPreferences("data1",MODE_PRIVATE);//data1：登录信息
        findName1=sp1.getString("name",null);//登录信息data中的用户名
        findPwd1=sp1.getString("pwd",null);//登录信息data中的密码
        if (inName.equals(findName)&&inPwd.equals(findPwd)){
            if (findName1==null||!findName1.equals(inName)){
                showAlertDialog();
            }else {
                success();
            }
        }else {
            showMsg("登录失败，用户名或密码错误");
        }
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(this)
                .setTitle("提示")
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("是否保存账号和密码？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor edit=sp1.edit();
                        edit.putString("name",inName);
                        edit.putString("pwd",inPwd);
                        edit.commit();
                        dialog.dismiss();
                        success();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        success();
                    }
                });
        alertDialog=builder.create();
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLACK);
        alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
    }

    //获取data存储的信息
    public void last(View view) {
        SharedPreferences sp1=getSharedPreferences("data1",MODE_PRIVATE);
        et_name.setText(sp1.getString("name",null));
        et_pwd.setText(sp1.getString("pwd",null));
    }

    //登录成功，跳转至主界面
    public void success(){
        showMsg("登录成功");
        Intent intent=new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra("name",inName);
        String id= String.valueOf(userId);
        intent.putExtra("userId",id);
        startActivity(intent);
    }
    //Toast
    private void showMsg(String s) {
        Toast.makeText(LoginActivity.this,s,Toast.LENGTH_SHORT).show();
    }
    private void init() {
        bt_register=findViewById(R.id.bt_register);
        bt_login=findViewById(R.id.bt_login);
        et_name=findViewById(R.id.et_name);
        et_pwd=findViewById(R.id.et_pwd);
        cb_pwd=findViewById(R.id.cb_pwd);
    }
}